#!/bin/bash

USER_PWD=`pwd`

# if there is only one argument which is not an option then it must be a file which we could try to open directly
if [ $# == 1 ]; then
	if echo "$1" | grep -q "^[^-]"; then
		file="$1"

		# local path? => make absolute
		if echo "$file" | grep -q "^[^\/]"; then
			file="$USER_PWD/$file"
		fi

		echo "open: $file" | nc -w 1 localhost 13231 2>/dev/null && exit 0
	fi
fi

if echo "$1" | grep -q "^-psn"; then
	shift 1
fi

cd `dirname $0`

MAC=""
if [ `uname` = "Darwin" ]
then
  MAC="-Xdock:name=JLatexEditor -Xdock:icon=tex-cookie.png"
fi

action=start
while [ "$action" = "start" ]; do
	java -Xmx200M $MAC "-Djlatexeditor.working_dir=$USER_PWD" -jar JLatexEditor.jar "$@"

	if [ "$?" != "255" ]; then
		action=end
	fi
done
